<template>
  <div>
    <el-card class="box-card" style="margin: 30px 0px">
      <el-table style="margin: 10px 0px" border :data="trademarkArr" stripe height="570">
        <el-table-column label="id" prop="userId" />
        <el-table-column label="名字" prop="username" />
        <el-table-column label="角色" prop="roleType" />
        <el-table-column label="钱包" prop="wallet" />
        <el-table-column label="状态" prop="status" />
        <el-table-column label="图片">
          <template #="{ row, $index }">
            <img :src="row.picture" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #="{ row, $index }">
            <el-button type="primary" size="small" icon="Select" @click="enableAUser(row.userId)" />
            <el-button
              type="primary"
              size="small"
              icon="CloseBold"
              @click="disableAUser(row.userId)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { listAllUsers, enableUser, disableUser } from '@/api/goods/index'
const router = useRouter()
import { ElMessage, UploadProps, formEmits } from 'element-plus'

const pageNo = ref<number>(1)
const limit = ref<number>(5)
const total = ref<number>(0)
const trademarkArr = reactive([])

//组件挂载：获取第一页数据
onMounted(() => {
  getHasTrademark()
})

const getHasTrademark = async () => {
  const result = await listAllUsers()
  trademarkArr.splice(0, trademarkArr.length, ...result)
  console.log(trademarkArr)
}
const enableAUser = async (id: string) => {
  const result = await enableUser(id)
  if (result.code == 0) {
    ElMessage({
      type: 'success',
      message: '操作成功',
    })
    getHasTrademark()
  } else {
    ElMessage({
      type: 'error',
      message: '操作失败',
    })
  }
}
const disableAUser = async (id: string) => {
  const result = await disableUser(id)
  if (result.code == 0) {
    ElMessage({
      type: 'success',
      message: '操作成功',
    })
    getHasTrademark()
  } else {
    ElMessage({
      type: 'error',
      message: '操作失败',
    })
  }
}

//删除品牌：传id发出请求、成功/失败后提示、显示当前界面
const removeTradeMark = async (id: number) => {
  const temp = id + ''
  const result: any = await deleteOne(temp)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除品牌成功',
    })
    getHasTrademark(pageNo.value, limit.value)
  } else {
    ElMessage({
      type: 'error',
      message: '删除品牌失败',
    })
  }
}

const navigateTo = (routeName: string) => {
  router.push({ name: routeName })
}
</script>

<style lang="scss" scoped></style>
